package com.xyy.kingdeek3cloud.task;


import com.xyy.kingdeek3cloud.service.PopService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
@Slf4j
public class SaticScheduleTask {
    @Autowired
    private PopService popService;
    //3.添加定时任务

    /**
     * 商品库存同步   一分钟执行一次
     */
    @Scheduled(cron = "0 0/1 * * * ? ")
    private void productStock() {
        long begin = System.currentTimeMillis();
        popService.syncProductStockToPop();
        long end = System.currentTimeMillis();
        long ms = end - begin;
        log.info("商品库存任务执行时间:{} 本次执行耗时: {} 毫秒", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), ms);
    }

    /**
     * 商品全量信息同步
     */
    @Scheduled(cron = "0 0 * * * ? ")
    private void productFullInfo() {
        long begin = System.currentTimeMillis();
        popService.syncProductFullToPop();
        long end = System.currentTimeMillis();
        long ms = end - begin;
        log.info("商品信息任务执行时间:{} 本次执行耗时: {} 毫秒", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), ms);
    }

    /**
     * 订单同步
     */
    @Scheduled(cron = "0 0/5 * * * ? ")
    public void orderPull() {
        long begin = System.currentTimeMillis();
        popService.syncOrderToMerchant();
        long end = System.currentTimeMillis();
        long ms = end - begin;

        log.info("订单同步任务执行时间:{} 本次执行耗时: {} 毫秒", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), ms);
    }

    /**
     * 客户信息上报
     */
    @Scheduled(cron = "0 0/10 * * * ? ")
    public void customerToErp() {
        long begin = System.currentTimeMillis();
        popService.syncCustomerToPop();
        long end = System.currentTimeMillis();
        long ms = end - begin;

        log.info("客户信息上报任务执行时间:{} 本次执行耗时: {} 毫秒", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), ms);
    }

    /**
     * 客户导入
     */
    @Scheduled(cron = "0 0/5 * * * ? ")
    //@PostConstruct
    public void customerToJd() {
        long begin = System.currentTimeMillis();
        popService.syncNoneOpenCustomerToMerchant();
        long end = System.currentTimeMillis();
        long ms = end - begin;

        log.info("客户导入任务执行时间:{} 本次执行耗时: {} 毫秒", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), ms);
    }

    /**
     * 订单分拣
     */
//    @Scheduled(cron = "0 0/3 * * * ? ")
//    public void OrderComp() {
//        popService.syncCompletedToPop();
//        log.info("订单分拣任务执行时间:{} ", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//    }

    /**
     * 出库明细
     */
    @Scheduled(cron = "0 0/10 * * * ? ")
    public void orderDetail() {
        long begin = System.currentTimeMillis();
        popService.syncOrderDeliveryDetails();
        long end = System.currentTimeMillis();
        long ms = end - begin;
        log.info("出库明细任务执行时间:{} 本次执行耗时: {} 毫秒", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()), ms);
    }

    /**
     * 心跳检测
     */
    @Scheduled(cron = "0 0/2 * * * ? ")
    @PostConstruct
    public void heartbeatDetection() {
        popService.syncHeartBeatMonitor();
        log.info("心跳检测任务执行时间:{} ", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    }
}